namespace OFX.Aggregator.WpfApplication
{
	using System.Collections.Generic;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Input;

	using Infrastructure;

	/// <summary>
	/// Interaction logic for AccountTypeSelectionPage.xaml
	/// </summary>
	public partial class AccountTypeSelectionPage : Page
	{
		#region Commands

		public static RoutedCommand StatementSourceTemplate_CreateStatementSource = new RoutedCommand("StatementSourceTemplate_CreateStatementSource", typeof(AccountTypeSelectionPage));

		#endregion

		public static readonly DependencyProperty StatementSourceTemplatesProperty = DependencyProperty.Register("StatementSourceTemplates", typeof(IList<StatementSourceTemplate>), typeof(AccountTypeSelectionPage), new UIPropertyMetadata(null));

		private readonly IIocContainerAbstraction container;

		public AccountTypeSelectionPage(IIocContainerAbstraction container)
		{
			this.container = container;
			var statementSourceTemplateRepository = container.Resolve<IStatementSourceTemplateRepository>();

			StatementSourceTemplates = statementSourceTemplateRepository.StatementSourceTemplates;

			InitializeComponent();
		}

		public IList<StatementSourceTemplate> StatementSourceTemplates
		{
			get { return (IList<StatementSourceTemplate>) GetValue(StatementSourceTemplatesProperty); }
			set { SetValue(StatementSourceTemplatesProperty, value); }
		}

		// Using a DependencyProperty as the backing store for StatementSourceTemplates.  This enables animation, styling, binding, etc...

		private void CanCreateStatementSource(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = (e.Parameter != null); }

		private void CreateStatementSource(object sender, ExecutedRoutedEventArgs e)
		{
			var selectedTemplate = (StatementSourceTemplate) e.Parameter;
			if (selectedTemplate != null)
			{
				var page = this.container.Resolve<Page>("AccountSetupPage", new Dictionary<string, object> {{"template", selectedTemplate}});

				NavigationService.Navigate(page);
			}
		}
	}
}